package com.muyu.product.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.product.domain.CommentInfo;
import com.muyu.product.domain.req.CommentInfoQueryReq;
import com.muyu.product.domain.req.CommentInfoSaveReq;
import com.muyu.product.domain.req.CommentInfoEditReq;
import com.muyu.product.service.CommentInfoService;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 商品评论Controller
 *
 * @author DongZeLiang
 * @date 2024-02-27
 */
@Api(tags = "商品评论")
@RestController
@RequestMapping("/comment")
public class CommentInfoController extends BaseController {
    @Autowired
    private CommentInfoService commentInfoService;

    /**
     * 查询商品评论列表
     */
    @ApiOperation("获取商品评论列表")
    @RequiresPermissions("product:comment:list")
    @GetMapping("/list")
    public Result<TableDataInfo<CommentInfo>> list(CommentInfoQueryReq commentInfoQueryReq) {
        startPage();
        List<CommentInfo> list = commentInfoService.list(CommentInfo.queryBuild(commentInfoQueryReq));
        return getDataTable(list);
    }

    /**
     * 导出商品评论列表
     */
    @ApiOperation("导出商品评论列表")
    @RequiresPermissions("product:comment:export")
    @Log(title = "商品评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommentInfo commentInfo) {
        List<CommentInfo> list = commentInfoService.list(commentInfo);
        ExcelUtil<CommentInfo> util = new ExcelUtil<CommentInfo>(CommentInfo.class);
        util.exportExcel(response, list, "商品评论数据");
    }

    /**
     * 获取商品评论详细信息
     */
    @ApiOperation("获取商品评论详细信息")
    @RequiresPermissions("product:comment:query")
    @GetMapping(value = "/{id}")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    public Result<CommentInfo> getInfo(@PathVariable("id") Long id) {
        return Result.success(commentInfoService.getById(id));
    }

    /**
     * 新增商品评论
     */
    @RequiresPermissions("product:comment:add")
    @Log(title = "商品评论", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增商品评论")
    public Result<String> add(@RequestBody CommentInfoSaveReq commentInfoSaveReq) {
        return toAjax(commentInfoService.save(CommentInfo.saveBuild(commentInfoSaveReq)));
    }

    /**
     * 修改商品评论
     */
    @RequiresPermissions("product:comment:edit")
    @Log(title = "商品评论", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    @ApiOperation("修改商品评论")
    public Result<String> edit(@PathVariable Long id, @RequestBody CommentInfoEditReq commentInfoEditReq) {
        return toAjax(commentInfoService.updateById(CommentInfo.editBuild(id,commentInfoEditReq)));
    }

    /**
     * 删除商品评论
     */
    @RequiresPermissions("product:comment:remove")
    @Log(title = "商品评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除商品评论")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = String.class, example = "1,2,3,4")
    public Result<String> remove(@PathVariable List<Long> ids) {
        return toAjax(commentInfoService.removeBatchByIds(ids));
    }
}
